package com.sky.loveshop.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.loveshop.entity.Category;
import com.sky.loveshop.entity.Product;
import com.sky.loveshop.entity.exception.BizException;
import com.sky.loveshop.entity.exception.BizExceptionEnum;
import com.sky.loveshop.entity.qo.ProductQO;
import com.sky.loveshop.entity.vo.ProductVO;
import com.sky.loveshop.mapper.ProductMapper;
import com.sky.loveshop.service.CategoryService;
import com.sky.loveshop.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xcy
 * @version V1.0
 * @description
 * @date 2024/8/18 0:06
 */
@Slf4j
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CategoryService categoryService;

    @Override
    public List<ProductVO> listProduct(ProductQO productQO) {
        List<ProductVO> list = productMapper.searchAll(productQO);
        List<Integer> categoryIds = list.stream().map(Product::getCategoryId).distinct().collect(Collectors.toList());
        List<Category> shops = categoryService.getCategoryListByIds(categoryIds);
        Map<Integer, String> nameMap = shops.stream().collect(Collectors.toMap(Category::getId, Category::getName));
        list.stream().forEach(s -> {
            s.setCategoryName(nameMap.get(s.getCategoryId()));
        });
        return list;
    }

    @Override
    public PageInfo<ProductVO> listProductPages(ProductQO productQO) {
        PageHelper.startPage(productQO.getPageNum(), productQO.getPageSize());
        List<ProductVO> list = productMapper.searchAll(productQO);
        if (CollectionUtil.isEmpty(list)) {
            return new PageInfo<>();
        }
        List<Integer> categoryIds = list.stream().map(Product::getCategoryId).distinct().collect(Collectors.toList());
        List<Category> shops = categoryService.getCategoryListByIds(categoryIds);
        Map<Integer, String> nameMap = shops.stream().collect(Collectors.toMap(Category::getId, Category::getName));
        list.stream().forEach(s -> {
            s.setCategoryName(nameMap.get(s.getCategoryId()));
        });
        return new PageInfo<>(list);
    }

    @Override
    public ProductVO getProductById(Integer id) {
        Product product = productMapper.getProductById(id);
        Category category = categoryService.getCategoryById(product.getCategoryId());
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product, productVO);
        productVO.setCategoryName(category.getName());
        return productVO;
    }

    @Override
    public ProductVO getProductByBarCode(String barCode, Integer shopId) throws BizException {

        // 通过商店id获取此商店卖的哪个品牌的商品，如区分【留恋趣】【小姐姐】,用于解决同一个条形码存在不同品牌的商品的情况。
        String brand = getBrandByShopId(shopId);

        Example example = new Example(Product.class);
        example.createCriteria().andEqualTo("barCode", barCode)
                .andEqualTo("brand", brand);
        Product product = productMapper.selectOneByExample(example);
        if (null == product) {
            log.error("getProductByBarCodeError 通过条形码获取不到商品 barCode={} brand={}", barCode, brand);
            throw new BizException(BizExceptionEnum.PRODUCT_NOT_EXIST_BY_BAR_CODE);
        }
        Category category = categoryService.getCategoryById(product.getCategoryId());
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product, productVO);
        productVO.setCategoryName(category.getName());
        return productVO;
    }

    /**
     * 通过商店id获取此商店卖的哪个品牌的商品，如区分【留恋趣】【小姐姐】,用于解决同一个条形码存在不同品牌的商品的情况。
     *
     * @param shopId 商店id
     * @return 品牌名称
     */
    private String getBrandByShopId(Integer shopId) {
        List llqShopIds = Arrays.asList(1, 2, 3, 4, 5); //严格与数据商店id对应
        List xjjShopIds = Arrays.asList(6);
        if (llqShopIds.contains(shopId)) {
            return "留恋趣";
        }
        if (xjjShopIds.contains(shopId)) {
            return "小姐姐";
        }
        return null;
    }

    @Override
    public ProductVO getProductByUpc(String upc) {
        Example example = new Example(Product.class);
        example.createCriteria().andEqualTo("upcCode", upc);
        Product product = productMapper.selectOneByExample(example);
        if (null == product) {
            log.error("getProductByUpcError 通过upc获取不到商品 upc={}", upc);
            return null;
        }
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product, productVO);
        return productVO;
    }

    @Override
    public List<Product> getProductsByIds(List<Integer> ids) {
        return productMapper.getProductsByIds(ids);
    }

    @Override
    public Integer addProduct(Product product) {
        return productMapper.insertOne(product);
    }

    @Override
    public Integer delProductById(Integer id) {
        return productMapper.delProductById(id);
    }

    @Override
    public Integer updateProductById(Product product) {
        return productMapper.updateProductById(product);
    }

    @Override
    public List<ProductVO> getProductsByUpcList(List<String> upcList) {
        return productMapper.getProductsByUpcList(upcList);
    }

    @Override
    public List<Integer> getProductIdsByName(String productName) {
        return productMapper.getProductIdsByName(productName);
    }
}
